package wt.hpad;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import wt.hpad.model.bean.Dish;
import wt.hpad.model.bean.DishDetail;
import wt.hpad.model.bean.TableInfo;
import wt.hpad.model.local.DBHelper;
import wt.hpad.model.remote.HttpRequest;
import wt.hpad.utils.CommonUtil;
import wt.hpad.utils.GsonUtil;

import static wt.hpad.ConstantFields.CART_CURRENT;
import static wt.hpad.utils.LogUtil.logI;
import static wt.hpad.utils.LogUtil.logILine;

/**
 * 测试
 * Created by wt on 2.2.
 */

public class Test {
    private static List<TableInfo> tableInfos;
    private static List<Dish> dishes;
    private static int randomT;
    private static int randomD;

    public static void initTestData() {
        tableInfos = DBHelper.queryAllTableInfo();
        dishes = DBHelper.queryAllDishs();
        CART_CURRENT = new ArrayList<>();

        randomT = tableInfos.size();
        randomD = dishes.size();

        logILine("Test", "initTestData");
        for (TableInfo item :
                tableInfos) {
            logI("Test", "initTestData |  ", "" + item.toString());
        }
        logILine("Test", "initTestData");
        for (Dish item :
                dishes) {
            logI("Test", "initTestData |  ", "" + item.toString());
        }
        logILine("Test", "initTestData");
    }

    public static void startOrder() {
        CART_CURRENT.clear();
        int rt = CommonUtil.randomInt(randomT);
        String tableID = tableInfos.get(rt).tableID;
        for (int i = 0; i < 1; i++) {
            int rd = CommonUtil.randomInt(randomD);
            Dish dish = dishes.get(rd);
//            DishDetail dishDetail = new DishDetail(0000,
//                    i + 1,
//                    "错误的菜名",
//                    dish.dishTypeCode,
//                    0,
//                    null);
//
//            CART_CURRENT.add(dishDetail);

            DishDetail dishDetail = new DishDetail(dish.dishID,
                    i + 1,
                    dish.dishName,
                    dish.dishTypeCode,
                    0,
                    null);
            CART_CURRENT.add(dishDetail);
            logILine("Test", "startOrder 本单包含:" + tableID);
            logI("Test", "startOrder |  ", "" + CART_CURRENT.toString());
            logILine("Test", "startOrder end");
        }

        String dishes = GsonUtil.toJsonString(CART_CURRENT);

        new HttpRequest().handleOrder(placeObsr(), tableID, "test", "108", dishes);
    }

    private static Observer<? super Integer> placeObsr() {

        return new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Integer value) {
                if (value == 200) { // 下单 成功
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        logI("Test", "run |  ", "" + "下单:test...");
                        startOrder();
                    }
                }, 500);
            }
        };
    }

}
